﻿#include "NozzleHolder.h"
#include <QDebug>

const double DEFAULT_NOZZLE_SPACING = 8.0; // 喷嘴间隔 8 mm
const double DEFAULT_NOZZLE_ORIG_X = 17.95; // 0,0 喷嘴的位置
const double DEFAULT_NOZZLE_ORIG_Y = 41.60;
const int DEFAULT_COL_COUNT = 8;
const int DEFAULT_ROW_COUNT = 4;

NozzleHolder::NozzleHolder()
{
    m_colCount = DEFAULT_COL_COUNT;
    m_rowCount = DEFAULT_ROW_COUNT;
    m_colSpacing = DEFAULT_NOZZLE_SPACING;
    m_rowSpacing = DEFAULT_NOZZLE_SPACING;

    m_origin_x = DEFAULT_NOZZLE_ORIG_X;
    m_origin_y = DEFAULT_NOZZLE_ORIG_Y;
}

void NozzleHolder::setNozzleZeroPos(double x_mm, double y_mm)
{
    m_origin_x = x_mm;
    m_origin_y = y_mm;
}

void NozzleHolder::getNozzleZeroPos(double &x_mm, double &y_mm)
{
    x_mm = m_origin_x;
    y_mm = m_origin_y;
}

void NozzleHolder::resetNozzleZeroPos()
{
    m_origin_x = m_default_x;
    m_origin_y = m_default_y;
}

void NozzleHolder::getColRow(double x_mm, double y_mm, int &row, int &col)
{
    double x = (x_mm - m_origin_x) / m_colSpacing;
    double y = (y_mm - m_origin_y) / m_rowSpacing;

    for(row = 0; row < m_rowCount; row ++)
    {
        for(col = 0; col < m_colCount; col ++)
        {
            if( fabs(x - col) < 0.5 && fabs(y - row) < 0.5 )
            {
                return;
            }
        }
    }
    row = -1;
    col = -1;
    return;
}

void NozzleHolder::getPosition(int row, int col, double &x_mm, double &y_mm)
{
    x_mm = m_origin_x + col * m_colSpacing;
    y_mm = m_origin_y + row * m_rowSpacing;
}

NozzleHolder::~NozzleHolder()
{

}

void NozzleHolder::loadSettings(QSettings &settings)
{
    m_colCount = settings.value("NozzleHolder/colCount", DEFAULT_COL_COUNT).toInt();
    m_rowCount = settings.value("NozzleHolder/rowCount", DEFAULT_ROW_COUNT).toInt();
    m_colSpacing = settings.value("NozzleHolder/colSpacing", DEFAULT_NOZZLE_SPACING).toDouble();
    m_rowSpacing = settings.value("NozzleHolder/rowSpacing", DEFAULT_NOZZLE_SPACING).toDouble();

    m_origin_x = settings.value("NozzleHolder/originX", DEFAULT_NOZZLE_ORIG_X).toDouble();
    m_origin_y = settings.value("NozzleHolder/originY", DEFAULT_NOZZLE_ORIG_Y).toDouble();

    m_default_x = settings.value("NozzleHolder/defaultX", DEFAULT_NOZZLE_ORIG_X).toDouble();
    m_default_y = settings.value("NozzleHolder/defaultY", DEFAULT_NOZZLE_ORIG_Y).toDouble();
}

void NozzleHolder::writeSettings(QSettings &settings)
{
    //qDebug() << "NozzleHolder/originX = " << m_origin_x;
    //qDebug() << "NozzleHolder/originY = " << m_origin_y;

    settings.setValue("NozzleHolder/originX", m_origin_x);
    settings.setValue("NozzleHolder/originY", m_origin_y);
}

void NozzleHolder::dumpSettings(QSettings &settings)
{
    settings.setValue("NozzleHolder/originX", m_origin_x);
    settings.setValue("NozzleHolder/originY", m_origin_y);

    settings.setValue("NozzleHolder/rowCount", m_rowCount);
    settings.setValue("NozzleHolder/colCount", m_colCount);
    settings.setValue("NozzleHolder/colSpacing", m_colSpacing);
    settings.setValue("NozzleHolder/rowSpacing", m_rowSpacing);

    settings.setValue("NozzleHolder/defaultX", m_default_x);
    settings.setValue("NozzleHolder/defaultY", m_default_y);
}
